import request from './request';
const server = (function () {
    request.addPlugins({
        // include: [''],
        // exclude: [''],
        beforeSend: function (pluginsData: any) {
            if (localStorage.getItem('token')) {
                const config: object = { headers: { Authorization: localStorage.getItem('token') } };
                pluginsData.newConfig = config;
            }
            return pluginsData;
        }
        // afterSend: function (res: any) {
        // 可以在这里对返回的数据进行加工修改
        //     return res;
        // }
    });

    // #region 浙里办相关
    // request.addPlugins({
    //     include: ['uploadFile'],
    //     beforeSend: function (res: any) {
    //         const formData = new FormData();
    //         formData.append('file', res.data.file);

    //         return { newData: formData, newUrl: `${res.url}?dir=${res.data.dir}` };
    //     },
    //     afterSend: function (res: any) {
    //         return res;
    //     }
    // });
    // request.addPlugins({
    //     include: ['uploadFileMgop'],
    //     beforeSend: function () {
    //         return { isFile: true };
    //     },
    //     afterSend: function (res: any) {
    //         return res;
    //     }
    // });
    //#endregion

    const registOverCbArr: any[] = [];
    request.registOver = function (cb: Function) {
        registOverCbArr.push(cb);
    };

    const apiFiles = import.meta.glob('./apiList/*.ts');
    Object.keys(apiFiles).forEach((fileName, index) => {
        const file = apiFiles[fileName];
        file().then((res: any) => {
            request.registApi(res.default);
            if (index == Object.keys(apiFiles).length - 1) {
                // registOverCb && registOverCb();
                registOverCbArr.forEach(fun => fun());
                request.isRegistOver = true;
            } // 判断所有接口是否注册完毕
        });
    });
    return request;
})();
export default server;
